package cn.dnui_xcz826.dnuifood_16110100826.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import cn.dnui_xcz826.dnuifood_16110100826.MainActivity;
import cn.dnui_xcz826.dnuifood_16110100826.R;
import cn.dnui_xcz826.dnuifood_16110100826.bean.UserBean;
import cn.dnui_xcz826.dnuifood_16110100826.listener.onRetrofitListener;
import cn.dnui_xcz826.dnuifood_16110100826.model.UserModel;


public class LoginAcitvity extends BaseActivity implements onRetrofitListener<UserBean>,View.OnClickListener{
    private UserModel model;
    private Button btlogin,btregister,btthree,btfind;
    private EditText username,password;
    private Switch sw;
    SharedPreferences sharedPreferences;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        readSP();
    }


    @Override
    void initViews() {
        layout_file= R.layout.activity_login;
        setLayout(layout_file);
        btlogin=findViewById(R.id.btlogin);
        btregister=findViewById(R.id.btregister);
        //btfind=findViewById(R.id.btfind);
       // btthree=findViewById(R.id.btthree);
        username=findViewById(R.id.usernamere);
        password=findViewById(R.id.userpass);
        sw=findViewById(R.id.sw);
    }

    @Override
    void initEvents() {
        btlogin.setOnClickListener(this);
        btregister.setOnClickListener(this);
        //btfind.setOnClickListener(this);
        //btthree.setOnClickListener(this);
    }

    @Override
    void initDatas() {

    }

    @Override
    public void onSuccess(UserBean object, int flag) {
        if (object.getUserid().equals("0")){
            Toast.makeText(LoginAcitvity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
        }else {
            saveUser(object.getUserid());
            Intent intent;
            intent = new Intent(LoginAcitvity.this, MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginAcitvity.this,"登录成功",Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onFailure(String msg) {
        Toast.makeText(LoginAcitvity.this,"网络错误",Toast.LENGTH_SHORT).show();
    }
    private void readSP() {

        String strname=sharedPreferences.getString("name",null);
        String strpass=sharedPreferences.getString("pass",null);
        Boolean remember=sharedPreferences.getBoolean("remember",false);
        if (remember){
            username.setText(strname);
            password.setText(strpass);
        }
        sw.setChecked(remember);

    }
    private void saveUser(String userid) {

        SharedPreferences.Editor editor=sharedPreferences.edit();

//        int user_id=Integer.parseInt(userid);
        editor.putString("user_id",userid);
        editor.putString("name",username.getText().toString());
        editor.putString("pass",password.getText().toString());
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btlogin:
                if (username.getText().toString().trim().equals("")||password.getText().toString().trim().equals("")){
                    Toast.makeText(LoginAcitvity.this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();

                }
                else {
                    model=new UserModel();
                    model.login(username.getText().toString(),password.getText().toString(),this);
                }
                break;
            case R.id.btregister:
                intent=new Intent(LoginAcitvity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
                break;
//            case R.id.btfind:
//                intent=new Intent(LoginAcitvity.this,RegisterActivity.class);
//                startActivityForResult(intent,2);
//                break;
//            case R.id.btthree:
//                intent=new Intent(LoginAcitvity.this,ThreeLoginActivity.class);
//                startActivityForResult(intent,3);
//                break;

        }


    }
    @Override
   protected void onActivityResult(int resquestCode,int resultCode,Intent data){
        super.onActivityResult(resquestCode,resultCode,data);
        String uname=data.getStringExtra("user");
        username.setText(uname);
        //password.setText(null);
   }
}
